package com.project.les;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.easy.facebook.android.facebook.Facebook;
import com.easy.facebook.android.facebook.LoginListener;
import com.project.les.view.elements.FeriadoViewElement;

public class ViewFeriadoActivity extends ListActivity implements LoginListener  {

	private FeriadoViewElement data;
	private TwitterPost twitterPost;
	private FacebookPost facebookPost;
	
	private Integer regId = -1;
	
	private Boolean twitterClicked = false;
	private Boolean facebookClicked = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_feriado);

		// Twitter:
		this.twitterPost = TwitterPost.getInstance();
		this.twitterPost.setCallerActivity(this);
		
		// Facebook:
		this.facebookPost = FacebookPost.getInstance();
		this.facebookPost.setCallerActivity(this);

		// Informações do registro:
		this.regId = this.twitterPost.getAndClearLastPostId();
		if (this.regId < 0)
			this.regId = getIntent().getExtras().getInt("id");
		
		this.data = DataHelper.getInstance().selectAll("id = " + this.regId).get(0);
		
		String[] arrayRight = { data.getName(), data.getDay() + "/"
				+ data.getMonth() + "/" + data.getYear(), data.getMissingDay(), data.getType() };
		
		setListAdapter(new ArrayAdapter<String>(this,
				R.layout.feriado_list_item, arrayRight) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View row;
				LayoutInflater inflater = (LayoutInflater) this.getContext()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				if (null == convertView) {
					row = inflater.inflate(R.layout.feriado_list_item, null);
				} else {
					row = convertView;
				}				

				TextView tvRight = (TextView) row.findViewById(R.id.name);
				tvRight.setTextColor(Color.parseColor("#000000"));
				tvRight.setText(getItem(position));
				
				TextView tvLeft = (TextView) row.findViewById(R.id.date);
				tvLeft.setTextColor(Color.parseColor("#000000"));
				if(position == 0)					
					tvLeft.setText("Nome: ");
				if(position == 1)					
					tvLeft.setText("Data: ");
				if(position == 2)					
					tvLeft.setText("Descrição: ");
				if(position == 3)					
					tvLeft.setText("Tipo: ");

				return row;
			}
		});

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (this.facebookClicked) {
			this.facebookPost.setFacebookData(data);
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		Uri uri = this.getIntent().getData();
		
		//if (this.twitterClicked) {
		if (uri != null) {
			
			String oauthVerifier = uri.getQueryParameter("oauth_verifier");
			
			this.twitterPost.onActivityResume(oauthVerifier);
			
			
			this.twitterClicked = false;
		} else if (this.facebookClicked) {
			this.facebookClicked = false;
		}
		
	}

	public void tweet(View v) {
		this.twitterClicked = true;
		String message = this.feriadoMessage();

		this.twitterPost.setLastPostId(this.regId);
		this.twitterPost.login();
		this.twitterPost.tweetMessage(message);
	}

	public void facebook(View v) {
		this.facebookClicked = true;

		this.facebookPost.login();
	}

	private String feriadoMessage() {
		String date = this.data.getDay() + "/" + this.data.getMonth() + "/"
				+ this.data.getYear();
		String result = "Dia " + date + " é feriado: "
				+ this.data.getName()
				+ "! (By Feriados) (Teste de aplicação em Android)";
		
		return result;
	}
	
	public void back(View v) {
		finish();
	}

	//@Override
	public void loginSuccess(Facebook facebook) {
		String message = this.feriadoMessage();
		this.facebookPost.loginSuccess(facebook, message);
	}

	//@Override
	public void logoutSuccess() {
		this.facebookPost.logoutSuccess();
		
	}

	//@Override
	public void loginFail() {
		this.facebookPost.loginFail();
		
	}
}
